const path = require('path')
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { DefinePlugin } = require('webpack')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const { VueLoaderPlugin } = require('vue-loader/dist/index')
module.exports = {
    target: "web",
    mode: 'production',
    // mode: 'development',
    devtool: 'source-map',
    entry: "./src/main.js",
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: "js/rieter.js"
    },
    devServer: {
        contentBase: "./public",
        hot: true,
        host: '127.0.0.1',
        port: 8090,
        open: true,
        // compress: true, // 压缩，一般本地不开启
        proxy: { // 本地开发处理跨域，使用proxy代理
            '/api': {
                target: 'http://localhost:8888',
                pathRewrite: {
                    '^/api': ''
                },
                secure: false,// 如果希望代理https的请求设置false
                changeOrigin: true
            }
        }
    },
    resolve: {
        extensions: ['.js', '.json', '.mjs', '.vue', '.ts', '.jsx', '.tsx'],
        alias: {
            '@': path.resolve(__dirname, './src'),
            'js': path.resolve(__dirname, './src/js')
        }
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader',
                    'postcss-loader'
                ]
            },
            {
                test: /\.less$/,
                use: [
                    'style-loader',
                    'css-loader',
                    'less-loader',
                    'postcss-loader'
                ],
            },
            {
                test: /\.(png|jpg|gif|svg|jpeg)$/,
                type: 'asset',
                generator: {
                    filename: "images/[name]_[hash:6][ext]"
                },
                parser: {
                    dataUrlCondition: {
                        maxSize: 10 * 1024
                    }
                }
            },
            {
                test: /\.(eot|ttf|woff2?)$/,
                type: 'asset/resource',
                generator: {
                    filename: 'font/[name]_[hash:6][ext]'
                }
            },
            {
                test: /\.js$/,
                loader: 'babel-loader'

            },
            {
                test: /\.vue$/,
                loader: 'vue-loader'
            }
        ]
    },
    plugins: [
        new CleanWebpackPlugin(),
        new HtmlWebpackPlugin({
            template: './public/index.html',
            title: '首页'
        }),
        new DefinePlugin({
            BASE_URL: "'./'",
            __VUE_OPTIONS_API__: true,
            __VUE_PROD_DEVTOOLS__: true
        }),
        new CopyWebpackPlugin({
            patterns: [
                {
                    from: 'public',
                    to: './',
                    globOptions: {
                        ignore: [
                            '**/index.html'
                        ]
                    }
                }
            ]
        }),
        new VueLoaderPlugin()
    ]
}